package org.eiahe.hr.performance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.eiahe.hr.performance.domain.PerformanceLevel;
import org.eiahe.hr.performance.mapper.PerformanceLevelMapper;
import org.eiahe.hr.performance.service.IPerformanceLevelService;
import org.ehe.common.core.utils.StringUtils;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * 绩效等级Service业务层处理
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class PerformanceLevelServiceImpl implements IPerformanceLevelService {

    private final PerformanceLevelMapper baseMapper;

    @Override
    public PerformanceLevel queryById(Long levelId) {
        return baseMapper.selectById(levelId);
    }

    @Override
    public List<PerformanceLevel> queryList(PerformanceLevel level) {
        LambdaQueryWrapper<PerformanceLevel> lqw = buildQueryWrapper(level);
        return baseMapper.selectList(lqw);
    }

    @Override
    public TableDataInfo<PerformanceLevel> queryPageList(PerformanceLevel level, PageQuery pageQuery) {
        LambdaQueryWrapper<PerformanceLevel> lqw = buildQueryWrapper(level);
        Page<PerformanceLevel> page = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    private LambdaQueryWrapper<PerformanceLevel> buildQueryWrapper(PerformanceLevel level) {
        LambdaQueryWrapper<PerformanceLevel> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(level.getLevelName()), PerformanceLevel::getLevelName, level.getLevelName());
        lqw.eq(StringUtils.isNotBlank(level.getStatus()), PerformanceLevel::getStatus, level.getStatus());
        lqw.orderByAsc(PerformanceLevel::getSortOrder);
        return lqw;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(PerformanceLevel level) {
        return baseMapper.insert(level) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(PerformanceLevel level) {
        return baseMapper.updateById(level) > 0;
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // 可以添加删除校验逻辑
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
